﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonUtils
{
    public class PathManager
    {
        public static string AppRoot;
        public static string AppDataRoot;
        public static string AppConfigPath;
        public static string AppLogPath;
        public static string AppConfigDbPath;
        /// <summary>
        /// 全局设置
        /// </summary>
        public static string AppConfigSettingFilePath;
        /// <summary>
        /// 开发者设置文件夹
        /// </summary>
        public static string AppDevelopSettingPath;
        /// <summary>
        /// 开发者设置
        /// </summary>
        public static string AppDevelopSettingFilePath;
        /// <summary>
        /// 应用版本信息
        /// </summary>
        public static string AppVerionFilePath;
        public static string AppVerionENFilePath;
        public static string AppConfigUserInfoFilePath;
        public static string TempPath;

        public static string UserProfilePath;
        public static string WinUserName;
        /// <summary>
        /// 是否使用xml文件作为mapper文件
        /// </summary>
        public static bool IsUseMapperXMLFile = false;

        public static void Init()
        {
            AppDataRoot = Environment.ExpandEnvironmentVariables(StringConstant.AppData); //系统应用数据文件夹
            UserProfilePath = Environment.ExpandEnvironmentVariables(StringConstant.UserProfile); //系统用户目录文件夹
            TempPath = Environment.ExpandEnvironmentVariables(StringConstant.Temp); //系统临时文件夹
            WinUserName = Environment.UserName; //Windows系统用户名

            #region 解决普通用户登录，使用管理员权限打开程序的问题
            string winUser = ApplicationLoader.GetCurrentActiveUser();
            if (!string.IsNullOrEmpty(winUser) && string.Compare(WinUserName, winUser, true) != 0)
            {
                WinUserName = winUser;

                winUser = string.Format("\\{0}\\", winUser);
                string processUser = string.Format("\\{0}\\", Environment.UserName);

                UserProfilePath = UserProfilePath.Replace(processUser, winUser);

                if (!FileFolderHelper.IsFolderExist(UserProfilePath))
                {
                    UserProfilePath = Environment.ExpandEnvironmentVariables(StringConstant.UserProfile);
                    WinUserName = Environment.UserName;
                }
                else
                {
                    AppDataRoot = AppDataRoot.Replace(processUser, winUser);
                    if (!FileFolderHelper.IsFolderExist(AppDataRoot))
                    {
                        AppDataRoot = Environment.ExpandEnvironmentVariables(StringConstant.AppData);
                        UserProfilePath = Environment.ExpandEnvironmentVariables(StringConstant.UserProfile);
                        WinUserName = Environment.UserName;
                    }
                }
            }
            #endregion

            AppDataRoot = Path.Combine(AppDataRoot, StringConstant.ProductName);
            createFolder(AppDataRoot);

            AppRoot = FileFolderHelper.GetCurrentPath(); // 当前软件根目录
            AppConfigPath = Path.Combine(AppDataRoot, StringConstant.ConfigFolder);
            createFolder(AppConfigPath);
            AppDevelopSettingPath = Path.Combine(AppConfigPath, StringConstant.DevelopSettingFolder);
            createFolder(AppDevelopSettingPath);
            AppLogPath = Path.Combine(AppDataRoot, StringConstant.LogFolder);
            createFolder(AppLogPath);
            string dbPath = Path.Combine(AppDataRoot, StringConstant.DbFolder);
            createFolder(dbPath);
            AppConfigDbPath = Path.Combine(dbPath, StringConstant.DbFileName);
            //用户账号信息-加密保存
            AppConfigUserInfoFilePath = Path.Combine(dbPath, StringConstant.UsersFileName);
            AppDevelopSettingFilePath = Path.Combine(AppDevelopSettingPath, StringConstant.DevelopSettingFileName);
            AppVerionFilePath = Path.Combine(AppDataRoot, StringConstant.VersionFileName);
            AppVerionENFilePath = Path.Combine(AppDataRoot, StringConstant.VersionENFileName);

            AppConfigSettingFilePath = Path.Combine(AppDataRoot, StringConstant.SettingFileName);
        }

        static void createFolder(string folder)
        {
            FileFolderHelper.CreateFolder(folder);
        }
    }
}
